using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.ChatRoom;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class PeopleList : Panel
	{
		public enum PeopleListMode
		{
			EnableAddMode,
			EnableDelMode,
			CommonModeMode
		}
		private const string TAG = "PeopleList";
		public const int UserForPeopleShow = 0;
		public const int UsedForShareCard = 1;
		private AddPeopleButton mAddPeopleBtn;
		private AddPeopleButton mDelPeopleBtn;
		public int ShowType;
		private bool bTapFromChild;
		public PeopleList.PeopleListMode originMode;
		public RoutedEventHandler AddPepleClick;
		private PeopleListPage plPage;
		private string delUserName;
		private string delNickName;
		private PeopleList.PeopleListMode mNowListMode = PeopleList.PeopleListMode.EnableDelMode;
		private List<IChatPeople> MemberCtrlList = new List<IChatPeople>();
		public int HerSpace = 36;
		public int VerSpace = 22;
		public int LineNum = 3;
		private EventWatcher mChatRoomEventWatcher;
		public PeopleList.PeopleListMode NowListMode
		{
			get
			{
				return this.mNowListMode;
			}
			set
			{
				this.mNowListMode = value;
				base.InvalidateArrange();
				base.InvalidateMeasure();
			}
		}
		public PeopleList()
		{
			base.add_Loaded(new RoutedEventHandler(this.PeopleList_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.PeopleList_Unloaded));
			this.mAddPeopleBtn = new AddPeopleButton();
			this.mAddPeopleBtn.add_Click(new RoutedEventHandler(this.mAddPeopleBtn_Click));
			base.get_Children().Add(this.mAddPeopleBtn);
			this.mDelPeopleBtn = new AddPeopleButton(true);
			this.mDelPeopleBtn.add_Click(new RoutedEventHandler(this.mDelPeopleBtn_Click));
			base.get_Children().Add(this.mDelPeopleBtn);
		}
		private void PeopleList_Unloaded(object sender, RoutedEventArgs e)
		{
			this.WatcherUnit();
		}
		private void PeopleList_Loaded(object sender, RoutedEventArgs e)
		{
			ChatPeopleButton.itemMode = ChatPeopleButton.PeopleButtonMode.commonMode;
			this.WatcherInit();
		}
		public void ResetDelState()
		{
			if (!this.bTapFromChild)
			{
				this.ShowAddAndDelBtn();
				return;
			}
			this.bTapFromChild = false;
		}
		private void HideAddAndDelBtn()
		{
			this.originMode = this.NowListMode;
			this.NowListMode = PeopleList.PeopleListMode.CommonModeMode;
			ChatPeopleButton.ChangeMode(ChatPeopleButton.PeopleButtonMode.delMode);
		}
		private void ShowAddAndDelBtn()
		{
			if (this.originMode != PeopleList.PeopleListMode.CommonModeMode)
			{
				this.NowListMode = this.originMode;
				ChatPeopleButton.ChangeMode(ChatPeopleButton.PeopleButtonMode.commonMode);
			}
		}
		private void mDelPeopleBtn_Click(object sender, RoutedEventArgs e)
		{
			this.bTapFromChild = true;
			if (this.ShowType == 1)
			{
				return;
			}
			this.HideAddAndDelBtn();
		}
		private void mAddPeopleBtn_Click(object sender, RoutedEventArgs e)
		{
			this.bTapFromChild = true;
			if (this.ShowType == 1)
			{
				return;
			}
			if (this.AddPepleClick != null)
			{
				this.AddPepleClick.Invoke(sender, e);
			}
		}
		public void GoToContactDetailPage(Contact contact)
		{
			if (ToolFunction.checkIsStrSystemPlugin(contact.strUsrName))
			{
				if (contact.strUsrName.CompareTo("fmessage") == 0)
				{
					ToolFunction.GotoPageForUserControl("/Source/UI/Contact/FriendsIntroducePluginView.xaml", null, false);
					return;
				}
			}
			else
			{
				object[] array = new object[]
				{
					contact
				};
				ContactDetailViewForAll.GotoContactDitailView(null, null, contact, ContactDetailFromType.FROM_CHATGROUP, false);
			}
		}
		private void DelChatPeopleBtn_Click(object sender, RoutedEventArgs e)
		{
			this.bTapFromChild = true;
			ChatPeopleButton chatPeopleButton = sender as ChatPeopleButton;
			if (chatPeopleButton.People == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(chatPeopleButton.People.UserName))
			{
				return;
			}
			this.delUserName = chatPeopleButton.People.UserName;
			this.delNickName = chatPeopleButton.People.NickName;
			if (ChatPeopleButton.itemMode != ChatPeopleButton.PeopleButtonMode.commonMode && !(this.delUserName == AccountMgr.curUserName))
			{
				BasePage rootPage = ToolFunction.GetRootPage();
				if (rootPage is PeopleListPage)
				{
					this.plPage = (rootPage as PeopleListPage);
				}
				if (this.plPage == null)
				{
					return;
				}
				string cuUser = this.plPage.cuUser;
				Contact contact = StorageMgr.contact.method_0(cuUser);
				if (contact.strChatRoomOwner != AccountMgr.curUserName)
				{
					MMMessageBox mMMessageBox = MMMessageBox.Show(strings.ChatRoomDeleteMemberNotOwner, true, "确定");
					mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnChatRoomDeleteMemberNotOwnerHandler);
					return;
				}
				List<string> list = new List<string>();
				list.Add(this.delUserName);
				ServiceCenter.sceneDelChatRoomMemberService.doScene(cuUser, list);
				this.plPage.showProgressCover(strings.NewMessage_MemberDeleting);
				return;
			}
			else
			{
				Contact contact2 = StorageMgr.contact.method_0(this.delUserName);
				if (contact2 == null)
				{
					return;
				}
				this.GoToContactDetailPage(contact2);
				return;
			}
		}
		public void OnChatRoomDeleteMemberNotOwnerHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			ChatPeopleButton.ChangeMode(ChatPeopleButton.PeopleButtonMode.commonMode);
			this.NowListMode = PeopleList.PeopleListMode.EnableAddMode;
		}
		public void AddPeople(string UserName, string NickName)
		{
			for (int i = 0; i < this.MemberCtrlList.get_Count(); i++)
			{
				if (this.MemberCtrlList.get_Item(i).People.UserName == UserName)
				{
					return;
				}
			}
			IChatPeople chatPeople;
			if (this.ShowType == 1)
			{
				chatPeople = new ShareCardItem();
			}
			else
			{
				chatPeople = new ChatPeopleButton();
				ChatPeopleButton chatPeopleButton = chatPeople as ChatPeopleButton;
				ChatPeopleButton expr_58 = chatPeopleButton;
				expr_58.DelPepleClick = (RoutedEventHandler)Delegate.Combine(expr_58.DelPepleClick, new RoutedEventHandler(this.DelChatPeopleBtn_Click));
			}
			chatPeople.People = new ChatPeopleInfo
			{
				UserName = UserName,
				NickName = NickName
			};
			int num = 0;
			for (int j = 0; j < this.MemberCtrlList.get_Count(); j++)
			{
				if (string.Compare(this.MemberCtrlList.get_Item(j).People.NickName, chatPeople.People.NickName) > 0)
				{
					num = j;
					IL_F7:
					this.MemberCtrlList.Insert(num, chatPeople);
					UIElement uIElement = chatPeople as UIElement;
					if (uIElement != null)
					{
						base.get_Children().Insert(num, uIElement);
					}
					return;
				}
				if (j == this.MemberCtrlList.get_Count())
				{
					num = this.MemberCtrlList.get_Count();
				}
			}
			goto IL_F7;
		}
		public void DelPeople(string UserName, string NickName)
		{
			IChatPeople chatPeople;
			if (this.ShowType == 1)
			{
				chatPeople = new ShareCardItem();
			}
			else
			{
				chatPeople = new ChatPeopleButton();
			}
			chatPeople.People = new ChatPeopleInfo
			{
				UserName = UserName,
				NickName = NickName
			};
			for (int i = 0; i < this.MemberCtrlList.get_Count(); i++)
			{
				if (string.Compare(this.MemberCtrlList.get_Item(i).People.UserName, chatPeople.People.UserName) == 0)
				{
					this.MemberCtrlList.Remove(chatPeople);
					base.get_Children().RemoveAt(i);
					return;
				}
			}
		}
		public void RefreshPeople(List<Contact> currentRoomInfo)
		{
			if (currentRoomInfo != null && currentRoomInfo.get_Count() > 0)
			{
				int i = this.MemberCtrlList.get_Count() - 1;
				IL_81:
				while (i >= 0)
				{
					bool flag = false;
					for (int j = 0; j < currentRoomInfo.get_Count(); j++)
					{
						if (this.MemberCtrlList.get_Item(i).People.UserName == currentRoomInfo.get_Item(j).strUsrName)
						{
							flag = true;
							IL_62:
							if (!flag)
							{
								this.MemberCtrlList.RemoveAt(i);
								base.get_Children().RemoveAt(i);
							}
							i--;
							goto IL_81;
						}
					}
					goto IL_62;
				}
				using (List<Contact>.Enumerator enumerator = currentRoomInfo.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Contact current = enumerator.get_Current();
						string nickName;
						if (current.strUsrName == AccountMgr.getCurAccount().strUsrName)
						{
							nickName = strings.ChatRoomMyself;
						}
						else
						{
							nickName = ContactHelper.GetContactDisplayName(current);
						}
						this.AddPeople(current.strUsrName, nickName);
					}
				}
				return;
			}
			this.ClearPeople();
		}
		public void ClearPeople()
		{
			if (this.MemberCtrlList.get_Count() + 1 != base.get_Children().get_Count())
			{
				return;
			}
			for (int i = this.MemberCtrlList.get_Count(); i > 0; i--)
			{
				base.get_Children().RemoveAt(i);
			}
			this.MemberCtrlList.Clear();
		}
		public void SetChatRoomUsername(string chatroomName)
		{
			this.NowListMode = PeopleList.PeopleListMode.CommonModeMode;
			if (string.IsNullOrEmpty(chatroomName))
			{
				return;
			}
			if (ContactMgr.getUserType(chatroomName) != ContactMgr.UserType.UserTypeChatRoom)
			{
				return;
			}
			List<Contact> chatRoomMemberContactList = ChatRoomMgr.getChatRoomMemberContactList(chatroomName);
			if (chatRoomMemberContactList != null && chatRoomMemberContactList.get_Count() >= 1)
			{
				this.RefreshPeople(chatRoomMemberContactList);
				return;
			}
			this.RefreshPeople(null);
			Log.smethod_1("PeopleList", "RefreshForNewMember:currentRoomInfo == null");
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			Size result = base.MeasureOverride(availableSize);
			if (base.get_Children().get_Count() <= 0)
			{
				return result;
			}
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				base.get_Children().get_Item(i).Measure(availableSize);
			}
			Size size;
			if (this.ShowType == 1)
			{
				size = this.GetSize(base.get_Children().get_Count() - 2, base.get_Children().get_Item(base.get_Children().get_Count() - 1).get_DesiredSize().get_Width(), base.get_Children().get_Item(base.get_Children().get_Count() - 1).get_DesiredSize().get_Height());
			}
			else
			{
				if (this.NowListMode == PeopleList.PeopleListMode.EnableDelMode)
				{
					size = this.GetSize(base.get_Children().get_Count(), this.mAddPeopleBtn.get_DesiredSize().get_Width(), this.mAddPeopleBtn.get_DesiredSize().get_Height());
				}
				else
				{
					if (this.NowListMode == PeopleList.PeopleListMode.EnableAddMode)
					{
						size = this.GetSize(base.get_Children().get_Count() - 1, this.mAddPeopleBtn.get_DesiredSize().get_Width(), this.mAddPeopleBtn.get_DesiredSize().get_Height());
					}
					else
					{
						size = this.GetSize(base.get_Children().get_Count() - 2, this.mAddPeopleBtn.get_DesiredSize().get_Width(), this.mAddPeopleBtn.get_DesiredSize().get_Height());
					}
				}
			}
			return size;
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			Size result = base.ArrangeOverride(finalSize);
			if (base.get_Children().get_Count() <= 0)
			{
				return result;
			}
			int num = 0;
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				double width = base.get_Children().get_Item(i).get_DesiredSize().get_Width();
				double height = base.get_Children().get_Item(i).get_DesiredSize().get_Height();
				if (base.get_Children().get_Item(i) is IChatPeople)
				{
					Size pos = this.GetPos(num, width, height);
					base.get_Children().get_Item(i).Arrange(new Rect(pos.get_Width(), pos.get_Height(), width, height));
					num++;
				}
				else
				{
					if (this.NowListMode == PeopleList.PeopleListMode.EnableAddMode)
					{
						if (i == base.get_Children().get_Count() - 1)
						{
							base.get_Children().get_Item(i).Arrange(new Rect(0.0, 0.0, 0.0, 0.0));
						}
						else
						{
							Size pos = this.GetPos(i, width, height);
							base.get_Children().get_Item(i).Arrange(new Rect(pos.get_Width(), pos.get_Height(), width, height));
						}
					}
					else
					{
						if (this.NowListMode == PeopleList.PeopleListMode.EnableDelMode)
						{
							Size pos = this.GetPos(i, width, height);
							base.get_Children().get_Item(i).Arrange(new Rect(pos.get_Width(), pos.get_Height(), width, height));
						}
						else
						{
							base.get_Children().get_Item(i).Arrange(new Rect(-440.0, -440.0, 0.0, 0.0));
						}
					}
				}
			}
			Size size;
			if (this.ShowType == 1)
			{
				size = this.GetSize(base.get_Children().get_Count() - 2, base.get_Children().get_Item(base.get_Children().get_Count() - 1).get_DesiredSize().get_Width(), base.get_Children().get_Item(base.get_Children().get_Count() - 1).get_DesiredSize().get_Height());
			}
			else
			{
				if (this.NowListMode == PeopleList.PeopleListMode.EnableDelMode)
				{
					size = this.GetSize(base.get_Children().get_Count(), this.mAddPeopleBtn.get_DesiredSize().get_Width(), this.mAddPeopleBtn.get_DesiredSize().get_Height());
				}
				else
				{
					if (this.NowListMode == PeopleList.PeopleListMode.EnableAddMode)
					{
						size = this.GetSize(base.get_Children().get_Count() - 1, this.mAddPeopleBtn.get_DesiredSize().get_Width(), this.mAddPeopleBtn.get_DesiredSize().get_Height());
					}
					else
					{
						size = this.GetSize(base.get_Children().get_Count() - 2, this.mAddPeopleBtn.get_DesiredSize().get_Width(), this.mAddPeopleBtn.get_DesiredSize().get_Height());
					}
				}
			}
			return size;
		}
		private Size GetPos(int index, double double_0, double double_1)
		{
			Size result = default(Size);
			int num = index / this.LineNum;
			int num2 = index % this.LineNum;
			result.set_Width((double)num2 * (double_0 + (double)this.HerSpace));
			result.set_Height((double)num * (double_1 + (double)this.VerSpace));
			return result;
		}
		private Size GetSize(int count, double double_0, double double_1)
		{
			Size result = default(Size);
			if (count <= 0)
			{
				count = 1;
			}
			int num = (count - 1) / this.LineNum;
			result.set_Height((double)(num + 1) * (double_1 + (double)this.VerSpace));
			result.set_Width((double)this.LineNum * double_0 + (double)((this.LineNum - 1) * this.HerSpace));
			return result;
		}
		private void WatcherInit()
		{
			this.mChatRoomEventWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onModifyChatPageEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DEL_CHATROOM_MEMBER_SUCCESS, this.mChatRoomEventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DEL_CHATROOM_MEMBER_ERR, this.mChatRoomEventWatcher);
		}
		private void WatcherUnit()
		{
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DEL_CHATROOM_MEMBER_SUCCESS, this.mChatRoomEventWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DEL_CHATROOM_MEMBER_ERR, this.mChatRoomEventWatcher);
			this.mChatRoomEventWatcher = null;
		}
		private void onModifyChatPageEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (this.plPage == null)
			{
				BasePage rootPage = ToolFunction.GetRootPage();
				if (rootPage is PeopleListPage)
				{
					this.plPage = (rootPage as PeopleListPage);
				}
			}
			this.plPage.hideProgressCover();
			if (evtArgs == null)
			{
				return;
			}
			Log.smethod_5("PeopleList", "onChatRoomEventHandler:" + evtArgs.mEventID);
			NetSceneDelChatRoomMemberResult netSceneDelChatRoomMemberResult = evtArgs.mObject as NetSceneDelChatRoomMemberResult;
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_NETSCENE_DEL_CHATROOM_MEMBER_SUCCESS:
				this.delUserName = netSceneDelChatRoomMemberResult.nameList.get_Item(0);
				this.DelPeople(this.delUserName, null);
				return;
			case EventConst.ON_NETSCENE_DEL_CHATROOM_MEMBER_ERR:
				if (netSceneDelChatRoomMemberResult == null)
				{
					MMMessageBox.Show(strings.ChatRoomDeleteMemberFail, true, "确定");
					Log.smethod_1("PeopleList", "netSceneAddChatRoomMemberResult == null");
					return;
				}
				if (netSceneDelChatRoomMemberResult.sceneDelChatRoomMember == null)
				{
					MMMessageBox.Show(strings.ChatRoomDeleteMemberFail, true, "确定");
					Log.smethod_1("PeopleList", "netSceneAddChatRoomMemberResult.sceneAddChatRoomMember == null");
					return;
				}
				MMMessageBox.Show(strings.ChatRoomDeleteMemberFail, true, "确定");
				return;
			default:
				return;
			}
		}
		private string getShowInfo(List<string> userlist, string formatStr)
		{
			string text = "";
			if (userlist != null && userlist.get_Count() != 0)
			{
				for (int i = 0; i < userlist.get_Count(); i++)
				{
					Contact contact = StorageMgr.contact.method_0(userlist.get_Item(i));
					if (i == userlist.get_Count() - 1)
					{
						text += ((contact == null) ? userlist.get_Item(i) : ContactHelper.GetContactDisplayName(contact));
					}
					else
					{
						text = text + ((contact == null) ? userlist.get_Item(i) : ContactHelper.GetContactDisplayName(contact)) + ",";
					}
				}
			}
			return text + formatStr;
		}
	}
}
